using DBUtil.SqlSegment;
using System;
using System.Collections.Generic;
using System.Text;

namespace DBUtil.Provider.MySql.SqlSegment
{
    /// <summary>
    /// 数据库字符串运算符
    /// </summary>
    public class MySqlStringSqlSegment : StringSqlSegment
    {
        public MySqlStringSqlSegment(DBAccess db) : base(db)
        {
        }

        public override string GetLength(string input, bool null2Zero = false)
        {
            if (null2Zero) return $"length(ifnull({input},''))";
            else return $"length({input})";
        }
        public override string Trim(string input) => $"trim({input})";
        public override string LeftTrim(string input) => $"ltrim({input})";
        public override string RightTrim(string input) => $"rtrim({input})";
        public override string Upper(string input) => $"upper({input})";
        public override string Lower(string input) => $"lower({input})";
        public override string ReplaceAll(string input, string oldStr, string newStr) => $"replace({input},'{oldStr}','{newStr}')";
        public override string SplitFirst(string input, string pattern) => $"substring_index({input}, {pattern}, 1)";
        public override string SplitLast(string input, string pattern) => $"substring_index({input}, {pattern}, -1)";
        public override string Add(string input, string input2) => $"concat({input},{input2})";
    }
}
